﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Qu.SeniorProject.Model
{
    public class DocumentRepository
    {
        public static List<Document> GetAll()
        {
            var spDB = new SeniorProjectEntities();
            return spDB.Documents.OrderBy(d => d.DisplayOrder).ToList();
        }

        public static Document Get(int docId)
        {
            var spDB = new SeniorProjectEntities();
            return spDB.Documents.Where(p => p.DocumentId == docId).FirstOrDefault();
        }

        public static ProjectDocument GetProjectDocument(int docId)
        {
            var spDB = new SeniorProjectEntities();
            return spDB.ProjectDocuments.Where(p => p.DocumentId == docId).FirstOrDefault();
        }

        public static IList<ProjectDocument> GetProjectDocuments(int projectId)
        {
            var spDB = new SeniorProjectEntities();
            return spDB.ProjectDocuments.Where(p => p.ProjectId == projectId).ToList();
        }

        public static IList<Deliverable> GetDueDeliverables(int projectId)
        {
            var spDB = new SeniorProjectEntities();
            var deliverables = spDB.Database.SqlQuery<Deliverable>(string.Format("GetDueDeliverables @ProjectId='{0}'", projectId)).ToList();
            return deliverables;
        }

        public static bool IsFallSemester()
        {
            bool isFallSemester = true;
            var spDB = new SeniorProjectEntities();
            if (spDB.ProjectPeriods.Where(p => p.Status == "Current" && p.ProposalExaminationEndDate < DateTime.Now).Count() == 1)
                isFallSemester = false;

            return isFallSemester;
        }
    }
}
